package logic

import (
	"context"
	"fmt"
	"medical-management-system/models/mysql"

	"medical-management-system/medicalrpc/internal/svc"
	"medical-management-system/medicalrpc/medicalrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *medicalrpc.LoginRequest) (*medicalrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var medical mysql.Medical
	err := mysql.DB.Debug().Where("account = ?", in.Account).Find(&medical).Error
	if medical.ID == 0 {
		return nil, fmt.Errorf("账号不存在，请注册后登录")
	}

	if err != nil {
		return nil, fmt.Errorf("登录异常")
	}

	if medical.Password != in.Password {
		return nil, fmt.Errorf("密码输入错误，请重新输入")
	}

	return &medicalrpc.LoginResponse{Id: int64(medical.ID)}, nil
}
